package cn.qdhlm.collectservice.common.helper;


import cn.qdhlm.collectservice.common.util.ComUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @Name: RedisHelper
 * @Description: RedisHelper
 * @Author: jiangcs
 **/
@Component
public class RedisHelper {
    @Autowired
    private RedisTemplate<String, ?> redisTemplate;
    private static RedisTemplate<String, ?> redisTemplateSta;

    @PostConstruct
    private void init() {
        redisTemplateSta = this.redisTemplate;
    }

    /**
     * 获取 value值
     *
     * @param key
     * @return
     */
    public static String get(final String key) {
        if (ComUtil.isEmpty(key)) {
            return null;
        }

        return redisTemplateSta.execute((RedisCallback<String>) connection -> {
            RedisSerializer<String> serializer = redisTemplateSta.getStringSerializer();
            byte[] byteKey = serializer.serialize(key);
            if (byteKey == null) {
                byteKey = new byte[0];
            }
            byte[] value = connection.get(byteKey);
            return serializer.deserialize(value);
        });
    }

    /**
     * 保存 key-value
     *
     * @param key
     * @param value
     * @return
     */
    private static Boolean set(final String key, final String value) {
        // 写入redis
        return redisTemplateSta.execute((RedisCallback<Boolean>) connection -> {
            RedisSerializer<String> serializer = redisTemplateSta.getStringSerializer();
            byte[] byteKey = serializer.serialize(key);
            if (byteKey == null) {
                byteKey = new byte[0];
            }
            byte[] byteVal = serializer.serialize(value);
            if (byteVal == null) {
                byteVal = new byte[0];
            }
            connection.set(byteKey, byteVal);
            return true;
        });
    }

    /**
     * 保存 key-value：不存在key，则保存成功
     *
     * @param key
     * @param value
     * @return
     */
    public static Boolean setNX(final String key, final String value) {
        return redisTemplateSta.execute((RedisCallback<Boolean>) connection -> {
            RedisSerializer<String> serializer = redisTemplateSta.getStringSerializer();
            byte[] byteKey = serializer.serialize(key);
            if (byteKey == null) {
                byteKey = new byte[0];
            }
            byte[] byteVal = serializer.serialize(value);
            if (byteVal == null) {
                byteVal = new byte[0];
            }
            return connection.setNX(byteKey, byteVal);
        });
    }

    /**
     * 删除
     *
     * @param key
     * @return
     */
    public static Boolean delete(final String key) {
        return redisTemplateSta.delete(key);
    }

    /**
     * 根据 key 计算 计数标识
     *
     * @param key
     * @param maxNum
     * @return
     */
    public static int nextSN(String key, Long maxNum) {
        Long num = redisTemplateSta.opsForValue().increment("SN_" + key, 1);
        if (num == null || num.compareTo(maxNum) >= 0) {
            set("SN_" + key, "1");
            num = 1L;
        }
        return num.intValue();
    }

    /**
     * 发送消息
     *
     * @param channel
     * @param message
     */
    public static void send(String channel, String message) {
        redisTemplateSta.convertAndSend(channel, message);
    }
}
